package com.hlcl.huaji.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.elcl.activity.BaseActivity;
import com.elcl.network.AppContext;
import com.elcl.util.StringUtils;
import com.elcl.util.viewutils.ViewUtils;
import com.elcl.util.viewutils.WindowUtils;
import com.elcl.widget.comp.toast.ToastUtils;
import com.google.gson.reflect.TypeToken;
import com.hlcl.huaji.R;
import com.hlcl.huaji.model.LoginResponse;
import com.hlcl.huaji.model.LoginResult;
import com.hlcl.huaji.server.NetCode;
import com.hlcl.huaji.server.OrderServer;

/**
 * Created by lichunlin on 2018/6/17.
 * 提现的界面
 */

public class WithDrawActivity extends BaseActivity {

    private long income;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_with_draw);

        WindowUtils windowUtils = new WindowUtils();
        windowUtils.fullScreen(WithDrawActivity.this);
        getUserInfo();
        income = loginResponse.getIncome();
        setText(R.id.tv_price_all, "可用余额:" + income + "元");
        initClickView();
    }

    private LoginResult loginResult;
    private LoginResponse loginResponse;

    private void getUserInfo() {
        String userInfo = AppContext.getUerInfo();
        if (StringUtils.isNotBlank(userInfo)) {
            loginResult = gson.fromJson(userInfo, new TypeToken<LoginResult>() {
            }.getType());
            if (null != loginResult) {
                loginResponse = loginResult.getjData();
                if (null != loginResponse) {
                    setText(R.id.tv_bank, loginResponse.getCardName());
                    setText(R.id.tv_bank_no, loginResponse.getCardNo());
                }
            }
        }
    }

    private String[] prompts = new String[]{"请输入金额"};

    private void initClickView() {
        setListener(R.id.tv_with_draw_all, new View.OnClickListener() {
            @Override
            public void onClick(View v) {//全部提现
                setText(R.id.edt_input, income + "");
            }
        });

        setListener(R.id.tv_with_draw, new View.OnClickListener() {
            @Override
            public void onClick(View v) {//提现
                String[] money = ViewUtils.isEdtNull(WithDrawActivity.this, prompts, R.id.edt_input);
                if (null != money) {
                    if (!money[0].equals("0")) {
                        OrderServer.getInstance().withDrawServer(netHandler, money[0]);
                    } else {
                        ToastUtils.showToast("请输入正确的提现金额");
                    }
                }
            }
        });

        setListener(R.id.ll_account, new View.OnClickListener() {//点击跳转绑定账户
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(WithDrawActivity.this, WithDrawAccountActivity.class), 0x111);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 0x111) {
                getUserInfo();
            }
        }
    }

    @Override
    protected void praseJson(int reqId, String json) {
        if (reqId == NetCode.ID_WITHDRAW) {//提现
            ToastUtils.showToast("提现成功");
            finish();
        }
    }
}
